package com.dongnam.quanly.common.controller;

import java.util.List;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.dao.TaiKhoanNganHangDao;
import com.dongnam.quanly.common.exceptions.DatabaseException;
import com.dongnam.quanly.common.model.TaiKhoanNganHang;
import com.dongnam.quanly.common.model.VatTuHangHoa;
import com.dongnam.quanly.common.persistence.HibernateUtil;

public class TaiKhoanNganHangController extends GenericController<TaiKhoanNganHang, String> {

	private static final Logger logger = LoggerFactory.getLogger(TaiKhoanNganHangController.class);

	public TaiKhoanNganHangController() {
		super(new TaiKhoanNganHangDao());
		// TODO Auto-generated constructor stub
	}

	@Override
	public TaiKhoanNganHangDao getDao() {
		// TODO Auto-generated method stub
		return (TaiKhoanNganHangDao) dao;
	}

	public long findCountByFilters() throws DatabaseException {
		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();

		long count;
		try {
			count = getDao().findCountByFilters();
			if (!isTransacted) {
				session.getTransaction().commit();
			}
			return count;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

	public List<TaiKhoanNganHang> findByFilters(int pageSize, int pageNumber, String sortOrder, String sortField)
			throws DatabaseException {
		logger.debug("findByFilters, pageSize: " + pageSize);

		boolean isTransacted = HibernateUtil.getCurrentSession().getTransaction().isActive();
		Session session = isTransacted ? HibernateUtil.getCurrentSession() : HibernateUtil.startCurrentSession();

		try {
			logger.debug("load (pageSize = {}, sortField = {}, sortOrder = {})", new Object[] { pageSize, sortField,
					sortOrder });
			List<TaiKhoanNganHang> list = getDao().findByFilters(pageSize, pageNumber, sortField, sortOrder);

			logger.debug("size " + list.size());
			if (!isTransacted) {
				session.getTransaction().commit();
			}

			return list;
		} catch (Throwable e) {
			rollbackConditionally(isTransacted, session);
			logger.debug("exception", e);
			throw new DatabaseException(e);
		}

	}

}
